<?php
 class sqlImport {

 	// Constructor
 	function sqlImport($SqlArchive, $host = false, $user = false, $pass = false,
 		$database = false, $check = false) {
 		$this->host = $host;
 		$this->database = $database;
 		$this->user = $user;
 		$this->pass = $pass;
 		$this->SqlArchive = $SqlArchive;
 		$this->check = $check;
 	}

 	// Conectar a la base de datos
 	function dbConnect() {
 		$this->con = @mysql_connect($this->host, $this->user, $this->pass);
 		if (!$this->con) {
 			$this->error = "<b>Error (dbConnect): " . mysql_error() . "</b>";
 		}
 	}

 	// Seleccionar base de datos
 	function select_db() {
 		$result = @mysql_select_db($this->database);
 		if (!$result) {
 			$this->error = "<b>Error (select_db): " . mysql_error() . "</b>";
 		}
 	}

 	// Importar datos
 	function import() {
 		if ($this->host) {
 			$this->dbConnect();
 			if ($this->error)
 				return;
 		}
 		if ($this->database) {
 			$this->select_db();
 			if ($this->error)
 				return;
 		}

 		if ($this->con !== false || $this->check) {
 			$lines = file($this->SqlArchive, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
 			$buffer = '';
 			foreach ($lines as $line) { 		
 				if (($line = trim($line)) == '')
 					continue;
 				if (substr(ltrim($line), 0, 2) == '--')
 					continue;
 				if (substr($line, -1) != ';') {

 					$buffer .= $line;
 					continue;
 				} else
 					if ($buffer) {
 						$line = $buffer . $line; 						
 						$buffer = '';
 					}

 				$line = substr($line, 0, -1);
 				if (!$this->check)
 					$result = mysql_query($line);
 				else {
 					echo substr($line, 0, 180) . ((strlen($line) > 180) ? "...<br>" : "<br>");
 					$this->error = "<b>No data has been written (check = true)</b>";
 				}

 				if (!$result and !$this->check) {
 					$this->error = "<b>Error (mysql_query): " . mysql_error() . "</b>";
 					return;
 				}
 			}
 		}
 	}
 }

?>